package com.gxitsky.controller;

import com.gxitsky.common.constants.SysConstants;
import com.gxitsky.entity.ServiceRequest;
import com.gxitsky.service.IService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @author gxing
 * @desc
 * @date 2021/2/26
 */
@RestController
public class WebServiceController {

    @Autowired
    private IService service;

    /**
     * CXF客户端动态调用
     *
     * @param serviceRequest
     * @return
     */
    @PostMapping("/cxfInvoke")
    public Object service(@Validated @RequestBody ServiceRequest serviceRequest) {
        this.validRequestParams(serviceRequest);
        return service.cxfInvoke(serviceRequest);
    }

    /**
     * SOAP连接调Web Service接口
     *
     * @param serviceRequest
     * @return
     */
    @PostMapping("/soapInvoke")
    public Object soapInvoke(@Validated @RequestBody ServiceRequest serviceRequest) {
        return service.soapInvoke(serviceRequest);
    }

    /**
     * @desc: 校验入参接口
     * @param: [serviceRequest]
     * @author: gxing
     * @date: 2021/3/2
     */
    private void validRequestParams(ServiceRequest serviceRequest) {
        if (!CollectionUtils.isEmpty(serviceRequest.getParamsMap())) {
            String xmlRootName = serviceRequest.getParamsMap().get(SysConstants.XML_ROOT);
            if (StringUtils.isBlank(xmlRootName)) {
                throw new RuntimeException("xml根节点XmlRoot不能为空");
            }

            if (CollectionUtils.isEmpty(serviceRequest.getParamList())) {
                serviceRequest.setParamList(new ArrayList<>());
            }
        }
    }
}
